import useStore from '@/stores/index' // 导入 store 类型
import axios from 'axios'

const API_URL = 'http://119.29.41.94:8080/system/user/profile/updatePwd'

export interface ChangePasswordPayload { 
  oldPassword: string
  newPassword: string
}
export const changePassword = async (payload: ChangePasswordPayload): Promise<RootObject> => {
  const store = useStore() // 获取 store 实例
  try {
    const response = await axios.put<RootObject>(API_URL, payload, {
      data: {payload},
      headers: {
        Authorization: `Bearer ${store.$state.token}` // 添加 Bearer token 到请求头
      }
    })
    return response.data
  } catch (error) {
    console.error('修改密码请求失败:', error)
    //处理错误，返回一个自定义的响应以便与页面交互
    return { msg: '网络错误', code: 500 } as RootObject // 假设 RootObject结构
  }
}

export interface RootObject {
  msg: string
  code: number
}
